package com.example.demo.service.impl;

import com.example.demo.entity.request.uiTestCaseReq.UITestCaseAddReq;
import com.example.demo.entity.request.uiTestCaseReq.UITestCaseUpdateReq;
import com.example.demo.enums.ElementTypeEnum;
import com.example.demo.mapper.UICaseStepMapper;
import com.example.demo.mapper.UITestCaseMapper;
import com.example.demo.entity.UICaseStep;
import com.example.demo.entity.UITestCase;
import com.example.demo.service.UITestCaseService;
import com.example.demo.utils.DateToStamp;
import com.example.demo.utils.PageInfoNew;
import com.example.demo.utils.SeleniumUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.example.demo.enums.ActionTypeEnum.*;

@Service
@Slf4j
public class UITestCaseServiceImpl implements UITestCaseService {
    @Resource
    UITestCaseMapper uiTestCaseMapper;

    @Resource
    UICaseStepMapper uiCaseStepMapper;

    @Override
    public void add(UITestCaseAddReq req) {
        UITestCase uiTestCase = new UITestCase();
        BeanUtils.copyProperties(req, uiTestCase);
        uiTestCase.setCreateTime(DateToStamp.getTimeStampLong());
        uiTestCase.setUpdateTime(DateToStamp.getTimeStampLong());
        uiTestCaseMapper.insert(uiTestCase);
    }

    @Override
    public void update(UITestCaseUpdateReq req) {
        UITestCase uiTestCase = new UITestCase();
        BeanUtils.copyProperties(req, uiTestCase);
        uiTestCase.setUpdateTime(DateToStamp.getTimeStampLong());
        uiTestCaseMapper.updateUITestCase(uiTestCase);
    }

    @Override
    public PageInfoNew<UITestCase> page(int pageNum, int pageSize, Long uiTestCaseSuiteId, String apiName, Long projectId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfoNew<>(uiTestCaseMapper.findByPage(projectId, uiTestCaseSuiteId, apiName));
    }

    @Override
    public void debug(Long id) {
        SeleniumUtils seleniumUtils = new SeleniumUtils();
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        List<UICaseStep> uiCaseStepList = uiCaseStepMapper.findByTestCaseId(id);
        //根据ActionType来判断使用哪个执行方法
        try {
            //每次执行都窗口最大化
            seleniumUtils.chromeDriverMaxSize(webDriver);
            uiCaseStepList.forEach(uiCaseStep -> {
                if (uiCaseStep.getActionType().equals(SEND_KEYS_ACTION.getName())) {
                    seleniumUtils.sendKeys(webDriver, ElementTypeEnum.valueOfType(uiCaseStep.getElementType()).getName()
                            , uiCaseStep.getElementString(), uiCaseStep.getContent());
                } else if (uiCaseStep.getActionType().equals(SUBMIT_ACTION.getName())) {
                    seleniumUtils.submit(webDriver, ElementTypeEnum.valueOfType(uiCaseStep.getElementType()).getName()
                            , uiCaseStep.getElementString(), uiCaseStep.getContent());
                } else if (uiCaseStep.getActionType().equals(CLICK_ACTION.getName())) {
                    seleniumUtils.click(webDriver, ElementTypeEnum.valueOfType(uiCaseStep.getElementType()).getName()
                            , uiCaseStep.getElementString(), uiCaseStep.getContent());
                } else if (uiCaseStep.getActionType().equals(CLEAR_ACTION.getName())) {
                    seleniumUtils.clear(webDriver, ElementTypeEnum.valueOfType(uiCaseStep.getElementType()).getName()
                            , uiCaseStep.getElementString(), uiCaseStep.getContent());
                } else if (uiCaseStep.getActionType().equals(GET_TEXT_ACTION.getName())) {
                    seleniumUtils.getText(webDriver, ElementTypeEnum.valueOfType(uiCaseStep.getElementType()).getName()
                            , uiCaseStep.getElementString());
                } else if (uiCaseStep.getActionType().equals(GET_ACTION.getName())) {
                    seleniumUtils.get(webDriver, uiCaseStep.getContent());
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            //报错后，依旧需要强制性关闭chromeDriver应用，避免应用一直存在
            seleniumUtils.quit(webDriver);
        }

    }

    @Override
    public void deleteById(Long id) {
        uiTestCaseMapper.uiTestCaseDelete(id);
    }
}
